package com.luoqiu.cinema.util;

import com.luoqiu.cinema.entity.Scorecard;
import com.luoqiu.cinema.entity.UserInfo;
import com.luoqiu.cinema.mapper.ScorecardMapper;
import com.luoqiu.cinema.service.UserInfoService;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class UserInfoUtil {

    @Resource
    UserInfoService userInfoService;
    @Resource
    private ScorecardMapper scorecardMapper;

    public UserInfo getInfo() {
        String username = SecurityUtils.getSubject().getPrincipal().toString();
        UserInfo login = userInfoService.login(username);
        return login;
    }

    public void score(Integer score) {
        Integer id = getInfo().getId();
        Scorecard scorecard = scorecardMapper.loadByUserId(id);
        if (null == scorecard) {
            scorecard = new Scorecard();
            scorecard.setScore(250);
            scorecard.setUserId(id);
            scorecardMapper.insertSelective(scorecard);
        } else {
            scorecardMapper.updateScore(score, id);
        }
    }
}
